package cn.stylefeng.guns.modular.gridsystem.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.gridsystem.entity.BizException;
import cn.stylefeng.guns.modular.gridsystem.entity.BizExceptionLog;
import cn.stylefeng.guns.modular.gridsystem.entity.BizFaultModel;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizExceptionLogParam;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizExceptionParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizExceptionLogService;
import cn.stylefeng.guns.modular.gridsystem.service.BizExceptionService;
import cn.stylefeng.guns.modular.gridsystem.service.BizFaultModelService;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayChannelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 故障表 控制器
 *
 * @author: ssy
 * @date: 2021/09/14 11:47
 */
@RestController
public class BizExceptionController {

    @Resource
    private BizExceptionService bizExceptionService;

    @Resource
    private BizExceptionLogService bizExceptionLogService;

    @Resource
    private BizGatewayChannelService bizGatewayChannelService;

    @Resource
    private BizFaultModelService faultModelService;

    /**
     * 分页查询故障表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizException/page")
    @BusinessLog(title = "故障表_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(BizExceptionParam param) {
        return new SuccessResponseData(bizExceptionService.pageList(param));
    }

    /**
     * 获取全部故障表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizException/list")
    @BusinessLog(title = "故障表_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BizExceptionParam bizExceptionParam) {
        return new SuccessResponseData(bizExceptionService.list(bizExceptionParam));
    }

    /**
     * 查看详情故障表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizException/detail")
    @BusinessLog(title = "故障表_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BizExceptionParam.detail.class) BizExceptionParam bizExceptionParam) {
        return new SuccessResponseData(bizExceptionService.detail(bizExceptionParam));
    }

    /**
     * 添加故障表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizException/add")
    @BusinessLog(title = "故障表_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(BizExceptionParam.add.class) BizExceptionParam bizExceptionParam) {
        bizExceptionService.add(bizExceptionParam);
        return new SuccessResponseData();
    }

    /**
     * 删除故障表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizException/delete")
    @BusinessLog(title = "故障表_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(BizExceptionParam.delete.class) BizExceptionParam bizExceptionParam) {
        bizExceptionService.delete(bizExceptionParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑故障表
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizException/edit")
    @BusinessLog(title = "故障表_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BizExceptionParam.edit.class) BizExceptionParam bizExceptionParam) {
        bizExceptionService.edit(bizExceptionParam);
        return new SuccessResponseData();
    }

    /**
     * 修改状态
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizException/changeStatus")
    @BusinessLog(title = "故障表_修改状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData changeStatus(@RequestBody @Validated(BizExceptionParam.changeStatus.class) BizExceptionParam bizExceptionParam) {
        bizExceptionService.changeStatus(bizExceptionParam);
        return new SuccessResponseData();
    }

    /**
     * 查询设备最新一条未处理故障
     *
     * @param deviceId
     * @return
     */
    @GetMapping("/bizException/getNewfaultFile")
    public ResponseData getNewfaultFile(Integer deviceId) {
        QueryWrapper<BizException> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", deviceId);
        queryWrapper.eq("status", 0);
        queryWrapper.orderByDesc("created_at");
        queryWrapper.last("limit 1");
        BizException bizException = bizExceptionService.getOne(queryWrapper);
        if (ObjectUtil.isNotNull(bizException)) {
            Integer id = bizException.getId();
            QueryWrapper<BizExceptionLog> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("exception_id", id);
            queryWrapper1.orderByDesc("created_at");
            queryWrapper1.last("limit 1");
            Map<String, Object> one = bizExceptionLogService.getMap(queryWrapper1);
            String errorName = bizException.getErrorName();
            QueryWrapper<BizFaultModel> faultModelQueryWrapper = new QueryWrapper<>();
            faultModelQueryWrapper.eq("fault_name", errorName);
            faultModelQueryWrapper.last("limit 1");
            BizFaultModel faultModel = faultModelService.getOne(faultModelQueryWrapper);
            if (ObjectUtil.isNotNull(faultModel)) {
                String faultFile = faultModel.getModelFile();
                one.put("faultFile", faultFile);
//                String created_at = one.get("created_at").toString().substring(0, 10);
//                String format = DateUtil.format(new Date(), "yyyy-MM-dd");
                /*if(format.equals(created_at)){
                    if(ObjectUtil.isNotEmpty(faultModel.getFaultGif())){
                        one.put("faultGif",faultModel.getFaultGif());
                        one.put("isfaultGif",true);
                    }
                }*/
                if (ObjectUtil.isNotEmpty(faultModel.getFaultGif())) {
                    one.put("faultGif", faultModel.getFaultGif());
                    one.put("isfaultGif", true);
                    one.put("errorName", errorName);
                }
            }
            return new SuccessResponseData(one);
        }
        return new SuccessResponseData();
    }


    @GetMapping("/bizException/getNewfaultList")
    public ResponseData getNewfaultList(Integer deviceId) {
        QueryWrapper<BizException> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", deviceId);
        queryWrapper.eq("DATE_FORMAT(created_at,'%Y-%m-%d')", DateUtil.today());
        queryWrapper.eq("status", 0);
        queryWrapper.orderByDesc("created_at");
        queryWrapper.last("limit 1");
        BizException bizException = bizExceptionService.getOne(queryWrapper);
        if (bizException != null) {
            Integer id = bizException.getId();
            BizExceptionLogParam exceptionLogParam = new BizExceptionLogParam();
            exceptionLogParam.setId(id);
            List<BizExceptionLog> list = bizExceptionLogService.list(exceptionLogParam);
            Set<String> channels = new HashSet<>();
            for (BizExceptionLog bizExceptionLog : list) {
                String gatewayChannel = bizExceptionLog.getGatewayChannel();
                channels.add(gatewayChannel);
            }
            return ResponseData.success(channels);
        }
        return ResponseData.success();
    }

    /**
     * 获取处理状态的数量
     *
     * @return
     */
    @GetMapping("/bizException/getExceptionStatus")
    public Map<String, Long> getExceptionStatus(Integer deviceId) {
        return this.bizExceptionService.countStatus(deviceId);
    }

    @GetMapping("/bizException/getExceptionInfo")
    @ApiOperation("查询故障数量")
    public List<Map<String, Object>> getExceptionInfo(Integer deviceId) {
        QueryWrapper<BizException> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("count(*) as value", "error_name as name");
        queryWrapper.eq("device_id", deviceId);
        queryWrapper.groupBy("error_name");
        List<Map<String, Object>> listMaps = bizExceptionService.listMaps(queryWrapper);
        return listMaps;
    }
}
